package com.erpanderp.wam.ac.model.service.project;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.project.ProjectAssignment;
import com.erpanderp.wam.ac.model.entity.project.ProjectAssignmentDetail;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.util.HibernateUtil;

public class ProjectAssignmentDAO {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//METHODS FOR DOMESTIC PROJECT ASSIGNMENT FORM//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	public String getLastGeneratedDomesticProjectAssignmentCode(){
		String queryString="select pa.projectAssignmentCode from ProjectAssignment pa";
	
		List<String> codeList = null;
		
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.getTransaction().begin();		
			Query query = session.createQuery(queryString);
			query.setCacheable(false);
			codeList = (List<String>) query.list();
			session.getTransaction().commit();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		
		String lastCode = null;
		for (String code : codeList) {
			
			if (lastCode == null) {
				lastCode = code;
				continue;
				
			}else if (Long.parseLong(code.substring(code.indexOf("-")+1)) > Long.parseLong(lastCode.substring(lastCode.indexOf("-")+1))) {
					lastCode = code;
					continue;				
			}
			System.out.println(lastCode + "============ " + code);
		}
		
		return lastCode;	
	}
	
	
	 public String getLastGeneratedProjectPerdiemFormCode(String employeeCode){	    	
		  Long count = null;	 		
		 	try{
		 			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		 			session.getTransaction().begin();
		 			String queryString = "select count(*) from ProjectPerdiem p where p.employee.employeeCode=:employeeCode";
		
		 			Query query = session.createQuery(queryString);
		 			query.setCacheable(false).setString("employeeCode",employeeCode);;
		 			count = (Long) query.uniqueResult();
		 			session.getTransaction().commit();
		 		} catch (HibernateException e) {
		 			e.printStackTrace();
		 		}
		 		System.out.println("Count value :"+count); 
		 		count = count+1;
		 		System.out.println("Affter Add : "+count);
		 		return count.toString();	 		
		     }


	public ProjectAssignment createProjectAssignment(ProjectAssignment projectAssignment){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(projectAssignment);
		session.getTransaction().commit();
		return projectAssignment;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	public ProjectAssignmentDetail createProjectAssignmentDetail(ProjectAssignmentDetail projectAssignmentDetail){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(projectAssignmentDetail);
		session.getTransaction().commit();
		return projectAssignmentDetail;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}

	public List<ProjectAssignment> getAllProjectAssignments(Long employeeId) {
		List<ProjectAssignment> allProjectAssignments = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from ProjectAssignment pa where pa.employee.employeeId=:employeeId").setLong("employeeId", employeeId);
			query.setCacheable(false);
			allProjectAssignments = (List<ProjectAssignment>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allProjectAssignments;

	}
	
	
	public List<ProjectAssignment> getProjectAssignmentsByEmployeeId(Long employeeId) {
		List<ProjectAssignment> allProjectAssignments = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from ProjectAssignment pa where pa.employee.employeeId =:employeeId");
			query.setCacheable(false).setLong("employeeId", employeeId);
			allProjectAssignments = (List<ProjectAssignment>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allProjectAssignments;
	}
	
	
	
	public List<ProjectAssignment> getProjectAssignmentsByEmployeeIdProjectType(Long employeeId,String projectType) {
		System.out.println("IN GETPROJECT ASSIGNMENAT");
		List<ProjectAssignment> allProjectAssignments = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery
					("from ProjectAssignment pa where pa.employee.employeeId =:employeeId and pa.projectType =:projectType").
			setLong("employeeId", employeeId).setString("projectType", projectType);
			allProjectAssignments =query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allProjectAssignments;
	}
	
	public ProjectAssignment getProjectAssignmentByProjectAssignmentId(Long projectAssignmentId) {
		System.out.println("IN PROJECT ASSIGNMENT BY PROJECT ASI");
		ProjectAssignment projectAssignment = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			projectAssignment = (ProjectAssignment) session.load(ProjectAssignment.class, projectAssignmentId);			
			session.getTransaction().commit();
			return projectAssignment;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public ProjectAssignment getProjectAssignmentByProjectAssignmentIdAndEmployeeId(String projectAssignmentCode,Long employeeId) {
		System.out.println("IN PROJECT ASSIGNMENT BY PROJECT ASI");
		System.out.println("PROJECT ASSIGNMENT ID"+projectAssignmentCode+" "+"EMP ID"+employeeId);
		ProjectAssignment projectAssignment = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from ProjectAssignment p where p.projectAssignmentCode =:projectAssignmentCode and p.employee.employeeId=:employeeId";
			Query query = session.createQuery(queryString).setString("projectAssignmentCode", projectAssignmentCode).setLong("employeeId", employeeId);
			projectAssignment = (ProjectAssignment) query.uniqueResult();	
			System.out.println("PROJECT ASSIGNMENT CODE IS "+projectAssignment.getProjectAssignmentCode());
			session.getTransaction().commit();
			return projectAssignment;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public ProjectAssignmentDetail getProjectAssignmentDetailByProjectAssignmentId(Long projectAssignmentId) {
		System.out.println("IN PROJECT ASSIGNMENT DETAIL BY PROJECT ASI");
		ProjectAssignmentDetail projectAssignmentDetail = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from ProjectAssignmentDetail pa where pa.projectAssignment.projectAssignmentId=:projectAssignmentId";
			Query query = session.createQuery(queryString).setLong("projectAssignmentId", projectAssignmentId);
			projectAssignmentDetail = (ProjectAssignmentDetail) query.uniqueResult();	
			System.out.println("PROJECT ASSIGNMENT DETAISL IS "+(projectAssignmentDetail == null));
			session.getTransaction().commit();
			return projectAssignmentDetail;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	
	public List<ProjectAssignment> getProjectAssignmentsByManagerId(Long managerId) {
		List<ProjectAssignment> allProjectAssignments = null;
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from ProjectAssignment pa where pa.employee.employeeId in (select er.employee.employeeId from EmployeeAndReporting er where er.projectManager.employeeId = :managerId)");
			query.setCacheable(false).setLong("managerId", managerId);
			allProjectAssignments = (List<ProjectAssignment>) query.list();
			session.getTransaction().commit();

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return allProjectAssignments;

	}

	public ProjectAssignment updateProjectAssignment(ProjectAssignment projectAssignment) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.AUTO);
			
			session.update(projectAssignment);
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return projectAssignment;
	}
	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//METHODS FOR INTERNATIONAL PROJECT ASSIGNMENT FORM//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

public String getLastGeneratedInternationalProjectAssignmentCode(){
String queryString="select pa.projectAssignmentCode from ProjectAssignment pa";

List<String> codeList = null;

try{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.getTransaction().begin();		
Query query = session.createQuery(queryString);
query.setCacheable(false);
codeList = (List<String>) query.list();
session.getTransaction().commit();
}catch(HibernateException e){
e.printStackTrace();
}

String lastCode = null;
for (String code : codeList) {

if (lastCode == null) {
lastCode = code;
continue;

}else if (Long.parseLong(code.substring(code.indexOf("-")+1)) > Long.parseLong(lastCode.substring(lastCode.indexOf("-")+1))) {
lastCode = code;
continue;				
}
System.out.println(lastCode + "============ " + code);
}

return lastCode;	
}

public static void main(String a[]){
System.out.println(new ProjectAssignmentDAO().getLastGeneratedInternationalProjectAssignmentCode());
System.out.println("PAINE50051-1".substring("PAINE50051-1".indexOf("-")+1));
}


public ProjectAssignment createInternationalProjectAssignment(ProjectAssignment projectAssignment){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(projectAssignment);
		session.getTransaction().commit();
		return projectAssignment;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
public ProjectAssignment updateInternationalProjectAssignment(ProjectAssignment projectAssignment) {
	try {
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		session.getTransaction().begin();
		session.setFlushMode(FlushMode.AUTO);
		
		session.saveOrUpdate(projectAssignment);
		session.getTransaction().commit();
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
	return projectAssignment;
}
}



